'''
Created on 12/03/2010

@author: Usuario
'''

import os
from xml.dom import minidom
from xml.parsers import expat as XmlException

from settings import DIRS

#classes Message type

class Message_type():
    
    def __init__(self, id, message_type):
        self.id = id
        self.message_type = message_type
    
    def edit(self, message_type):
        self.id = id
        self.message_type = message_type
        
    def save_to_xml_file(self, myxml):
        nivel = myxml.createElement("message_type")
        nivel.setAttribute('id', self.id)
        nivel.setAttribute('message_type', self.message_type)
        return nivel
        
class Message_types():
    
    def __init__(self):
        self.message_types = {}
        self.message_type_id = 0
        
    def add(self, message_type, message_type_id = None):
        try:
            if message_type_id:
                m = Message_type(message_type_id, message_type)
                if message_type_id > self.message_type_id :
                    self.message_type_id = message_type_id + 1
            else:
                m = Message_type(self.message_type_id, message_type)
                self.message_type_id += 1
            self.message_types[self.message_type_id] = m
        except:
            print 'Error adding new Message_type into dictionary', message_type
            return False
        return True
        
    def edit(self, message_type_id, message_type):
        try:
            m = self.message_types[message_type_id]
            m.edit(Message_type)
        except:
            print 'Error editing Message_type from dictionary', message_type_id, message_type
            return False
        return True
    
    def delete(self, message_type_id):
        try:
            m = self.message_types.pop(message_type_id)
        except:
            print 'Error deleting Message_type from dictionary', message_type_id
            return False
        return True
    
    def get_all(self):
        return (('1', 'Recommendation'), ('2', 'Congratulation'))
        return self.message_types
    
    def get_by_id(self, message_type_id):
        try:
            m = self.message_types[message_type_id]
        except:
            print 'Error getting Message_type', message_type_id
            return None
        return m
    
    def save_xml_file(self, myxml):
        nivel1 = myxml.createElement('message_types')
        for key, value in self.message_types.items():
            nivel2 = value.save_xml_file(myxml)
            nivel1.appendChild(nivel2)
        return nivel1
    
    def load_from_xml_file(self, myxml):
        nivel1 = myxml.getElementsByTagName('message_types')
        for nivel2 in nivel1.getElementsByTagName('message_type'):
            self.add(nivel2.getAttribute('message_type'), message_type_id = nivel2.getAttribute('message_type_id'))
        return nivel1
        

# classes Recommendation():

class Recommendation():
    
    def __init__(self, id, message_type_id, message, value_from, value_to, indicator):
        self.id = id
        self.message_type_id = message_type_id
        self.message = message
        self.value_from = value_from
        self.value_to = value_to
        self.indicator = indicator
        
    def edit(self, message_type_id, message, value_from, value_to, indicator):
        self.id = id
        self.message_type_id = message_type_id
        self.message = message
        self.value_from = value_from
        self.value_to = value_to
        self.indicator = indicator
        
    def save_to_xml_file(self, myxml):
        nivel = myxml.createElement("recomendation")
        nivel.setAttribute('id', self.id)
        nivel.setAttribute('message_type_id', self.message_type_id)
        nivel.setAttribute('message', self.message)
        nivel.setAttribute('value_from', self.value_from)
        nivel.setAttribute('value_to', self.value_to)
        nivel.setAttribute('indicator', self.indicator)
        return nivel
        

class Recommendations():
    
    def __init__(self):
        self.recommendations = {}
        self.message_types = Message_types()
        self.recommendation_id = 0
        self.message_type_id = 0
        
    def message_type_add(self, message_type):
        return self.message_types.add(message_type)
    
    def message_type_edit(self, message_type_id, message_type):
        return self.message_types.edit(message_type_id, message_type)
    
    def message_type_delete(self, recommendation_id):
        return self.message_types.delete(recommendation_id)
    
    def message_type_get_all(self):
        return self.message_types.get_all()
    
    def message_type_get_by_id(self, message_type_id):
        return self.message_types.get_by_id(message_type_id)
    
    def add(self, message, message_type_id, value_from, value_to, indicator, recommendation_id = None):
        try:
            
            if recommendation_id:
                r = Recommendation(recommendation_id, message_type_id, message, value_from, value_to, indicator)
                if recommendation_id > self.recommendation_id :
                    self.recommendation_id = recommendation_id + 1
            else:
                r = Recommendation(self.recommendation_id, message_type_id, message, value_from, value_to, indicator)
                self.recommendation_id += 1
            self.recommendations[self.recommendation_id] = r
        except: 
            print 'Error adding Recommendation into dictionary', message, message_type_id, value_from, value_to
            return False
        return True
        
    def edit(self, message_type_id, message, value_from, value_to):
        try:
            r = self.recommendations[self.recommendation_id]
            r.edit(self.recommendation_id, message_type_id, message, value_from, value_to)
        except:
            print 'Error editing Recommendation from dictionary', message_type_id, message, value_from, value_to
            return False
        return True
    
    def delete(self, recommendation_id):
        try:
            r = self.recommendations.pop(recommendation_id)
        except:
            print 'Error deleting Recommendation from dictionary', recommendation_id
            return False
        return True
    
    def get_all(self):
        return self.recommendations
    
    def get_by_id(self, recommendation_id):
        try:
            r = self.recommendations[recommendation_id]
        except:
            print 'Error getting Recommendation form dictionary', recommendation_id
            return None
        return r
    
    def get_by_message_type_id(self, message_type_id):
        try:
            result = {}
            for key, value in self.recommendations.items():
                if value.message_type_id == message_type_id:
                    result[key] = value
        except:
            print 'Error getting Recommendations by message type id from dictionary', message_type_id
            return None
        return result
    
    def save_to_xml_file(self):
        try:
            filepath = DIRS["RECOMMENDATION_MEDIA_ROOT"] + 'recommendations1.xml'
            if os.path.isfile(filepath):
                myxml = minidom.Document()
                nivel1 = myxml.createElement("recommendations")
                
                for key, value in self.recommendations.items():
                    nivel2 = value.save_to_xml_file(myxml)
                    nivel1.appendChild(nivel2)
                
                nivel1.appendChild( self.message_types.save_xml_file(myxml) )
                
                fp = open(filepath, "w")
                myxml.writexml(fp, "\t", "\t", "\n", "UTF-8")
                
            else:
                print 'Error finding the xml file path', filepath
                return False
        except XmlException.ExpatError: 
            print 'Error parsing xml file'
            return False
        except:
            print 'Error saving to xml file all Recommendations'
            return False
        return True
        
    def load_from_xml_file(self):
        try:
            filepath = DIRS["RECOMMENDATION_MEDIA_ROOT"] + 'recommendations1.xml'
            if os.path.isfile(filepath):
                myxml = minidom.parse(filepath)
                nivel1 = myxml.getElementsByTagName('recommendations')
                for nivel2 in nivel1.getElementsByTagName('recommendation'):
                    self.add(nivel2.getAttribute('message'), nivel2.getAttribute('message_type_id'), 
                             nivel2.getAttribute('value_from'), nivel2.getAttribute('value_to'), 
                             nivel2.getAttribute('indicator'), recommendation_id = nivel2.getAttribute('recommendation_id'))
                self.message_types.load_from_xml_file(myxml)
            else:
                print 'Error finding the xml file path', filepath
                return False
        except XmlException.ExpatError: 
            print 'Error parsing xml file'
            return False
        except:
            print 'Error loading from xml file all Recommendations'
            return False
        return True
                
            

if __name__ == '__main__':
    pass
